#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
用户注册登录
"""
from lib.hash import encrypt_md5
from lib import log

# 用户信息：示例密码是 123
USER_DICT = {'eric': '202cb962ac59075b964b07152d234b70', 'alex': '202cb962ac59075b964b07152d234b70'}
# 当前登录用户
CURRENT_USER = None


def register():
    """
    用户注册
    :return:
    """
    print('用户注册')
    while True:
        user = input('请输入用户名(N返回上一级)：')
        if user.upper() == 'N':
            return
        pwd = input('请输入密码：')
        if user in USER_DICT:
            print('用户已经存在，请重新输入。')
            continue
        USER_DICT[user] = encrypt_md5(pwd)
        print('%s 注册成功' % user)
        # print(USER_DICT)


def login():
    """
    用户登录
    :return:
    """
    print('用户登录')
    global CURRENT_USER
    if not CURRENT_USER:
        while True:
            user = input('请输入用户名(N返回上一级)：')
            if user.upper() == 'N':
                return
            pwd = input('请输入密码：')
            if user not in USER_DICT:
                print('用户名不存在')
                continue

            encrypt_password = USER_DICT.get(user)
            if encrypt_md5(pwd) != encrypt_password:
                print('密码错误')
                continue
            print('登录成功')
            log.logger.debug('登录成功',)
            CURRENT_USER = user
            return
    else:
        print('您已登录')
        return
